package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.RedisConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private IUserService userService;

    /*因为我们后面有共同关注的业务，所以这里用户在关注和取消关注的时候，数据也需要放到redis的set集合里面*/
    /*第一个参数是博主的userId，第二个参数是关注还是取消关注*/
    public Result follow(Long followUserId,Boolean isFollow){
        /*1.获取当前登录用户的id*/
        Long userId = UserHolder.getUser().getId();
        /*2.构造redis里面set集合的key,根据当前登录用户的userId来构造*/
        String key=RedisConstants.USER_FOLLOWS+userId;
        /*3.判断isFollow是true还是false; isFollow是true表示此次操作是关注，isFollow是false表示此次操作是取消关注*/
        /*4.取消关注，需要删除set集合里面的数据，还需要删除数据库里面的数据*/
        if(!isFollow){
            boolean flag = remove(new QueryWrapper<Follow>().eq("user_id", userId.toString())
                    .eq("follow_user_id", followUserId));
            if(flag){
                stringRedisTemplate.opsForSet().remove(key, followUserId.toString());
            }
        }
        /*5.关注，需要增添set集合里面的数据，还需要在数据库里面增添数据*/
        if(isFollow){
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(followUserId);
            boolean flag = save(follow);
            if(flag){
                stringRedisTemplate.opsForSet().add(key, followUserId.toString());
            }
        }
        return Result.ok();
    }

    /*判断当前博主是否被当前登录用户关注过*/
    @Override
    public Result isFollow(Long followUserId) {
        /*1.获取当前登录用户id*/
        Long userId = UserHolder.getUser().getId();
        /*2.查看数据库里面是否有记录*/
        Integer count = query().eq("user_id", userId).eq("follow_user_id", followUserId).count();
        if(count>0){
            return Result.ok(true);
        }
        return Result.ok(false);
    }

    /*查询当前登录用户和博主是否有共同关注*/
    /*下面的参数是博主id*/
    /*这里用到的是redis里面set集合求取交集*/
    @Override
    public Result followCommons(Long id) {
        /*1.获取到当前登录用户的id*/
        Long userId = UserHolder.getUser().getId();
        /*2.根据登录用户id构造key*/
        String key1=RedisConstants.USER_FOLLOWS+userId;
        /*3.根据博主id构造key*/
        String key2=RedisConstants.USER_FOLLOWS+id;
        /*4.在set集合里面求交集*/
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(intersect.isEmpty() || intersect==null){
            return Result.ok(Collections.emptyList());
        }
        /*5.解析数据，得到求到的user的集合*/
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        /*6.根据ids查询用户*/
        List<UserDTO> userDTOS = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOS);
    }

}
